package com.zytlifeservice.controller.pay;

import com.qianmi.open.api.ApiException;
import com.zytlifeservice.domain.Result;
import com.zytlifeservice.domain.pay.TrianRechargeOrder;
import com.zytlifeservice.exception.UserException;
import com.zytlifeservice.service.pay.TrainService;
import com.zytlifeservice.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 火车票控制类
 * Created by Administrator on 2017/6/3.
 */
@RestController
@RequestMapping(value = "/train")
public class TrainController {
    @Autowired
    private TrainService trainService;

    //查询火车票列表
    @PostMapping(value = "/trainList")
    public Result findbyTrainList(String from, String date, String to) throws Exception {
        return trainService.findbyTrainList(from, date, to);
    }


    @PostMapping(value = "/queryInfo")
    public Result queryInfo(String subject, Long userId, String from, String to, String date, String trainNumber, String contactName, String contactTel, String passagers, String itemIdTrain, String startTime, String endTime) {
        try {
            TrianRechargeOrder queryInfo = trainService.queryInfo(subject, userId, from, to, date, trainNumber, contactName, contactTel, passagers, itemIdTrain, startTime, endTime);
            return ResultUtil.success(queryInfo, "查询成功");
        } catch (ApiException e) {
            e.printStackTrace();
            throw new UserException("查询失败", 303);
        }
    }


    @PostMapping(value = "/createAlipayOrder")
    public Result createAlipayOrder(String outTradeNo, long userId) {
        String[] result = trainService.createAlipayOrder(outTradeNo, userId);
        if ("success".equals(result[0])) {
            return ResultUtil.success(result[1], "预下单成功");
        } else {
            throw new UserException(result[1], 202);
        }
    }


    @PostMapping(value = "/createWxOrder")
    public Result createWxPayOrder(String outTradeNo, long userId, String ip) {
        String[] result = trainService.createWxOrder(outTradeNo, userId, ip);
        if ("success".equals(result[0])) {
            return ResultUtil.success(result[1], "预下单成功");
        } else {
            throw new UserException(result[1], 202);
        }
    }


    @PostMapping(value = "/queryTickets")
    public Result queryTickets(String identityCard) {
        return ResultUtil.success(trainService.queryTickets(identityCard), "查询成功");
    }

    @PostMapping(value = "/refund")
    public Result refund(String orderNos, String refund_free) {
        boolean refund = trainService.refund(orderNos, refund_free);
        if (refund) {
            return ResultUtil.success();
        } else {
            return ResultUtil.error(203, "退票失败，请重试或联系客服");
        }
    }

    @PostMapping(value = "/testPay")
    public Result testPay(String outTradNo) {
        trainService.pay(outTradNo);
        return ResultUtil.success();
    }
}
